#!/usr/bin/env python
#encoding=utf8

import os,sys
import fnmatch

'''查找文件'''
def find_files(topdir, pattern):
	for path, dirname, filelist in os.walk(topdir):
		for name in filelist:
			print name
			if fnmatch.fnmatch(name, pattern):
				yield os.path.join(path, name)

#打开压缩文件
import gzip, bz2
def opener(filenames):
	for name in filenames:
		if name.endswith(".gz"): f = gzip.open(name)
		elif name.endswith('*.bz2'): f = bz2.BZ2File(name)
		else: f = open(name)
		yield f

#linux cat命令
def cat(filelist):
	for f in filelist:
		for line in f:
			yield line

#linux grep 命令
def grep(pattern, lines):
	for line in lines:
		if pattern in line:
			yield line


#管道方式编程来查看日志
logs = find_files("/Users/alala-macbook/Logs", "*.log")
files = opener(logs)
lines = cat(files)
pylines = grep("DB", lines)
for line in pylines:
	sys.stdout.write(line)